﻿Public Class LogError

	Public Shared Function ReadException(ByVal ex As Exception) As String
		Try
			Dim sb As New Text.StringBuilder
			sb.AppendLine("Date Time : " & Now.ToString)
			'sb.AppendLine("---------------------------------------------------------------------")
			'sb.AppendLine("Exception : " & ex.ToString)
			sb.AppendLine("---------------------------------------------------------------------")
			sb.AppendLine("Exception Message : " & ex.Message)
			sb.AppendLine("---------------------------------------------------------------------")
			sb.AppendLine("Exception Source : " & ex.Source)
			sb.AppendLine("---------------------------------------------------------------------")
			sb.AppendLine("Exception StackTrace : " & ex.StackTrace)
			sb.AppendLine("---------------------------------------------------------------------")
			If ex.InnerException IsNot Nothing Then
				sb.AppendLine(FindInnerException(ex.InnerException, 1))
			End If

			Return sb.ToString
		Catch exError As Exception
			Return String.Empty
		End Try
	End Function

	Public Shared Function AddLogException(ByVal ex As Exception) As String
		Try
			Dim sb As New Text.StringBuilder
			sb.AppendLine("Date Time : " & Now.ToString)
			'sb.AppendLine("---------------------------------------------------------------------")
			'sb.AppendLine("Exception : " & ex.ToString)
			sb.AppendLine("---------------------------------------------------------------------")
			sb.AppendLine("Exception Message : " & ex.Message)
			sb.AppendLine("---------------------------------------------------------------------")
			sb.AppendLine("Exception Source : " & ex.Source)
			sb.AppendLine("---------------------------------------------------------------------")
			sb.AppendLine("Exception StackTrace : " & ex.StackTrace)
			sb.AppendLine("---------------------------------------------------------------------")
			If ex.InnerException IsNot Nothing Then
				sb.AppendLine(FindInnerException(ex.InnerException, 1))
			End If

			Dim log As New SS.ApplicationBlock.Log(SS.ApplicationBlock.Config.GetWebConfig("LogPath"))
			log.AddText(sb.ToString, SS.ApplicationBlock.Log.MesssageType.Error)
			log.Write()
			log = Nothing
			Return sb.ToString
		Catch exError As Exception
			Return String.Empty
		End Try
	End Function

	Private Shared Function FindInnerException(ByVal innerException As Exception, ByVal level As Integer) As String

		If innerException.InnerException IsNot Nothing Then
			FindInnerException = FindInnerException(innerException.InnerException, level + 1)
		Else
			Dim sb As New Text.StringBuilder
			sb.AppendLine("---------------- Inner Exception Level " & level.ToString & " ----------------")
			sb.AppendLine("(" & level.ToString & ") Exception : " & innerException.ToString)
			sb.AppendLine("---------------------------------------------------------------------")
			sb.AppendLine("(" & level.ToString & ") Exception Message : " & innerException.Message)
			sb.AppendLine("---------------------------------------------------------------------")
			sb.AppendLine("(" & level.ToString & ") Exception Source : " & innerException.Source)
			sb.AppendLine("---------------------------------------------------------------------")
			sb.AppendLine("(" & level.ToString & ") Exception StackTrace : " & innerException.StackTrace)
			sb.AppendLine("-------------------------------------------------------------------------------")
			Return sb.ToString
		End If

	End Function

End Class
